<?php

namespace app\admin\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;


class Users extends Base 
{
    public  $model;

    public function __construct()
    {
   $this->model = new \app\admin\model\Users;
        parent::__construct();
        $this->aliasPost = ["id"=>"id","name"=>"depart_name"];
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {//
//
        //1 禁用 0启用
        //启用账户搜索
        $where["a.state"] = 0;
      return   $this->baseIndexTab($where);
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function disenable()
    {//
//
        //启用账户搜索
        $where["a.state"] = 1;
        return   $this->baseIndexTab($where);
    }



    private function baseIndexTab($where=[]){
        /**
         *   可配置搜索  用户ID 手机号码 用户名 时间 代理ID
         */
        if(isset($this->user_id)&&!empty($this->user_id)){
            $where['a.id']=$this->user_id;
        }

        if(isset($this->agent_key_no)&&!empty($this->agent_key_no)){
            $where['ag.agent_key_no']=$this->agent_key_no;
        }
        if(isset($this->mobile)&&!empty($this->mobile)){
            $where['a.mobile']=['like','%'.$this->mobile.'%'];
        }
        if(isset($this->user_name)&&!empty($this->user_name)){
            $where['iv.user_name']=['like','%'.$this->user_name.'%'];
        }
        $t = 60*60*24-1;
        if(isset($this->start_time)||isset($this->end_time))
        {
            if(isset($this->start_time)&&isset($this->end_time)&&($this->start_time==$this->end_time)){

                $this->end_time = $this->start_time;
                $where['a.created_at']=['between',[strtotime($this->start_time),strtotime($this->end_time)+$t]];
            }else{
                $where['a.created_at']=['between',[isset($this->start_time)?strtotime($this->start_time):strtotime("1970/1/1"),isset($this->end_time)+$t?strtotime($this->end_time):strtotime("3030/11/1")]];
            }


        }
    /*    if(isset($this->agent_id)){
            $where['a.agent_id'] = $this->agent_id;
        }*/
        //时间的处理

        $res = $this->model->user_page($where);
        return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);
    }

    public function read($id)
    {
      $res =   $this->model->where(['id'=>$id])->find();
      return   sucessJson($this->ecLang("query"),$res);
    }


    /**
     *  改变员工 禁用 启用
     */
    public function changestate(){


       $res =  $this->model->where("id",$this->id)->update(['state'=>$this->state]);
        if(empty($res)){
            return failJson("修改用户失败");
        }
        return sucessJson("修改用户成功");
    }

    /**
     *  更改用户归属
     */
    public function change(){

        if($this->agents_ids!="all"){
                return failJson("不是管理员暂时不能修改归属");
        }
        $data = delwithData($this);
        if(empty($data)){
            return failJson("请提交数据");
        }

        $res = $this->model->save($data,['id'=>$this->id]);
        $resUser = $this->model->where(['id'=>$this->id])->find();
        if(!isset($this->staff_id)){
            if($resUser['staff_id']){
                $staffModel = new \app\admin\model\AdminStaff();
                $staffModel->where("id",$resUser['staff_id'])->update(['agent_id'=>$this->agent_id]);
            }
        }


        if(empty($res)){
            return failJson("修改失败");
        }
        return sucessJson("修改归属成功");

    }


    /**
     *  修改实名认证的信息
     */
    public function cinfo(){

        $arr = ["user_name","sex","id_number","yidongdianhua","youbian","dianziyouxiang","zhuzhi","zicguimo1","zicguimo2","touzhijingli","touzhijingli2","jiaoyishouyiren","buliang","total","daan","grade","age"];


        $users_invest = new \app\admin\model\UsersInvest();

        $data = $this->RealData;


        if(empty($data)){
                return failJson("数据不能为空");
        }

       $res = $users_invest->save($data,['user_id'=>$this->RealData['user_id']]);


        if(empty($res)){
            return failJson("修改失败");
        }
        return sucessJson("修改成功");


     /*   `user_name` varchar(15) DEFAULT NULL COMMENT '姓名',
  `sex` varchar(10) DEFAULT NULL COMMENT '性别',
  `id_number` varchar(35) DEFAULT NULL COMMENT '证件号码',
  `yidongdianhua` varchar(15) DEFAULT NULL COMMENT '移动电话',
  `youbian` varchar(35) DEFAULT NULL COMMENT '邮编',
  `dianziyouxiang` varchar(35) DEFAULT NULL COMMENT 'dianziyouxiang',
  `zhuzhi` varchar(35) DEFAULT NULL COMMENT '住址',
  `zicguimo1` varchar(5) DEFAULT '1' COMMENT '资产规模',
  `zicguimo2` varchar(5) DEFAULT NULL COMMENT '资产规模',
  `touzhijingli` varchar(5) DEFAULT NULL,
  `touzhijingli2` varchar(5) DEFAULT '1' COMMENT '投资经历',
  `jiaoyishouyiren` varchar(5) DEFAULT NULL COMMENT '1本人 2他人',
  `buliang` varchar(5) DEFAULT NULL COMMENT '不良诚信记录',
  `total` decimal(11,2) DEFAULT NULL,
  `daan` varchar(255) DEFAULT NULL,
  `grade` varchar(10) DEFAULT NULL COMMENT '等级',
  `age` varchar(11) DEFAULT NULL,
  `image` varchar(255) DEFAULT NULL COMMENT '风险签名',
  `htimage` varchar(255) DEFAULT NULL COMMENT '合同签名',*/

    }





    public function save()
    {

        $rows = $this->model->where(['mobile'=>$this->mobile])->count();
        if($rows>0){
            return   failJson($this->ecLang("mobilehas"));
        }

        $sid =  Request::instance()->token( '__token__',  'md5');
        $salt = substr($sid,0,4);
        $password = "666666a";
        $data = [
            "mobile"=>$this->mobile,
            "password"=>md5($password.$salt),
            "user_name"=>$this->user_name,
            "slat"=>$salt,
            "secure"=>$password,
             "resiger_ip"=>Request::instance()->ip(),
            "resiger_time"=>time(),
        ];

        $insert_id = $this->model->save($data);
        if(empty($insert_id))
        {
            return   failJson($this->ecLang("failInsert"));
        }

        return   sucessJson($this->ecLang("scuessInsert"));
    }




    public function recharge(){

        $UsersRecharge = model("UsersRecharge");
        if(!isset($this->bank_id)){
                $this->bank_id=0;
        }
        $data = [
            "deposit"=>abs($this->deposit),
            "pay_no"=>ordersn(),
            "user_id"=>$this->user_id,
            "bank_id"=>$this->bank_id,
            "type"=>1,
        ];
        $res =  $UsersRecharge->save($data);
        $UserModel = new \app\np\model\UsersInvest();
        $Users =  $UserModel->where("user_id",$res['user_id'])->find();
        $ContentFormt = Config::get("senddx")['recharge'];
        $content =  sprintf($ContentFormt,$Users['user_name'],$data['pay_no'],$this->deposit);
        $result = !empty($res)?"充值成功":"充值失败";
        w_s("resnd",["content"=>"{$content},{$result}","user_id"=>$this->user_id]);
        if(!$res){
            return   failJson($this->ecLang("failInsert"));
        }
        return sucessJson($this->ecLang("scuessInsert"));

        //充值
    }


    //获取验证信息
    public function idcards(){
        $UsersInvest = model("UsersInvest");
        if(isset($this->user_id)){
            $where['user_id'] = $this->user_id; //用户id
        }else{
            return failJson("该用户未输入验证信息");
        }


        //时间的处理
        $row = $UsersInvest->where($where)->find();
        if(empty($row)){
                return failJson("该用户未输入验证信息");
        }
        return sucessJson("查询成功",$row);

    }

    //用户信息修改
    public function basic(){
        $UsersInvest = model("UsersInvest");
        $where['user_id'] = $this->user_id; //用户id

        //时间的处理
        $row = $UsersInvest->where($where)->find();
        if(empty($row)){
            return failJson("用户异常");
        }
        $data = [
            "status"=>1
        ];
        //审核通过
        $res = $UsersInvest->save($data,['user_id'=>$this->user_id]);
        $ContentFormt = Config::get("senddx")['basic'];
        $content =  sprintf($ContentFormt,$row['user_name']);
        $result = !empty($res)?"实名认证成功":"实名认证失败";
        w_s("resnd",["content"=>"{$content},{$result}","user_id"=>isset($res['user_id'])?$res['user_id']:0]);
        if(!$res){
            return   failJson("实名认证失败");
        }
        return sucessJson($this->ecLang("实名认证成功"));
    }


    //用户信息修改
    public function basicfail(){
        $UsersInvest = model("UsersInvest");
        $where['user_id'] = $this->user_id; //用户id

        //时间的处理
        $row = $UsersInvest->where($where)->find();
        if(empty($row)){
            return failJson("用户异常");
        }
        $data = [
            "status"=>2
        ];
        //审核通过
        $res = $UsersInvest->save($data,['user_id'=>$this->user_id]);
        if($res){
            $content = "用户".$row['user_name'];
            w_s("resnd",["content"=>$content.","."实名认证审核通过","user_id"=>$this->user_id]);
            return  sucessJson("实名认证审核不通过");
        }

        return  failJson($this->ecLang("操作异常"));
    }


    public function banklist(){
        $where['user_id'] = $this->user_id;
        //时间的处理
        $UsersBank = model("UsersBank");
        $res = $UsersBank->bank_page($where);
        return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);
    }


}
